'use strict'

define(['jquery', 'bootstrap', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.edit();
        this.bindCoach();
        this.changeTime();
        this.createStudent();
        this.drop(data.refundPrice);
        this.transfer();
        this.stopCamp();
    },
    
    edit: function() {
        $(document).on('shown.bs.modal', '#group-round-edit', function(e){
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })

        })
      },
      
      bindCoach: function() {
          $(document).on('shown.bs.modal', '#bind-coach', function(e){
              
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })

          })
        },
        
    changeTime: function(status) {
        $(document).on('shown.bs.modal', '#change-time', function(e){

            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      },
      
      createStudent: function() {
          $(document).on('shown.bs.modal', '#student-create', function(e){
              
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })

          })
        },
          
      drop: function(refundPrice) {
          $(document).on('shown.bs.modal', '#course-drop', function(e){
        	  
        	  $('select[name="isRefund"]').change(function() {
        		  var isRefund = $(this).val();
            	  var $price = $('input[name="refundPrice"]');
            	  if(isRefund === 'false') {
            		  $('.price').addClass('hide');
            		  $price.removeAttr("data-validate").val("");
            	  } else {
            		  $('.price').removeClass('hide');
            		  $price.attr("data-validate", "required").val(refundPrice);
            	  }
        	  })

        	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
          })
        },
      
      transfer: function(status) {
          $(document).on('shown.bs.modal', '#course-transfer', function(e){
              
              $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })    
          })
        },
        
      stopCamp: function(status) {
          $(document).on('shown.bs.modal', '#course-stop-camp', function(e){

              $(e.currentTarget).find("form").shiftform()
          })
        }
      
  }
})
